package com.steam.splace;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import dao.DataAccess;

import thread.DoThread;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class SignUpActivity extends Activity {
	int stt=1;

    private final int signup_tag =5;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup_activity);
		
	}
	public void onClick(View view){
		CheckBox cb=(CheckBox)this.findViewById(R.id.cbAgree);
		if(cb.isChecked()){
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
		TextView txtPassword = (TextView) findViewById(R.id.txtPassword);
		TextView txtRePassword = (TextView) findViewById(R.id.txtRePassword);
		if(txtEmail.getText()!=null&&!txtEmail.getText().toString().trim().equals("")&&txtPassword.getText()!=null&&!txtPassword.getText().toString().trim().equals("")&&txtRePassword.getText()!=null&&!txtRePassword.getText().toString().trim().equals(""))
		{params.add(new BasicNameValuePair("email", txtEmail.getText().toString()));
		params.add(new BasicNameValuePair("password",txtPassword.getText().toString()));
		params.add(new BasicNameValuePair("repassword",txtRePassword.getText().toString()));
		ArrayList l=new ArrayList();
		l.add(signup_tag);
		l.add(params);
	DoThread thread=new DoThread(this);
	thread.execute(l); }
		else {Toast toast=Toast.makeText(this, "Email va password khong duoc de trong!",
				Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();}
		}
		else {Toast toast=Toast.makeText(this, "Ban phai dong y voi dieu khoan su dung!",
				1000);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menunologin, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		if (item.getItemId() == R.id.menuSearch) {
			if (!this.getClass().getSimpleName().equals("SearchActivity")) {

				Intent searchIntent = new Intent(this, SearchActivity.class);
				startActivity(searchIntent);
			}
		} else if (item.getItemId() == R.id.menuProfile) {
			if (!this.getClass().getSimpleName().equals("ProfileActivity")) {
				Intent profileIntent = new Intent(this, ProfileActivity.class);
				startActivity(profileIntent);
			}
		}
		else if(item.getItemId() == R.id.SignOut){
			DataAccess da=new DataAccess(this);
			try {
				da.open();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			da.signOut();
			da.close();
			this.finish();
			Intent loginIntent = new Intent(this, MainActivity.class);
			startActivity(loginIntent);
		}
		else if(item.getItemId() == R.id.menuSignUp){
			if (!this.getClass().getSimpleName().equals("SignUpActivity")) {

				Intent signUpActivity = new Intent(this, SignUpActivity.class);
				startActivity(signUpActivity);
			}
		}
		else if(item.getItemId() == R.id.menuForgot){
			if (!this.getClass().getSimpleName().equals("ForgotPasswordActivity")) {
				Intent forgotPasswordActivity = new Intent(this, ForgotPasswordActivity.class);
				startActivity(forgotPasswordActivity);
			}
		}
		else if(item.getItemId() == R.id.menuAddPlace){
			if (!this.getClass().getSimpleName().equals("AddPlaceActivity")) {
				Intent addPlaceActivity = new Intent(this, AddPlaceActivity.class);
				startActivity(addPlaceActivity);
			}
		}
		else if(item.getItemId() == R.id.menuSignIn){
			if (!this.getClass().getSimpleName().equals("MainActivity")) {
				Intent mainActivity = new Intent(this, MainActivity.class);
				startActivity(mainActivity);
			}
		}
		else if(item.getItemId() == R.id.menuSearchArea){
			if (!this.getClass().getSimpleName().equals("SearchByAreaActivity")) {
				Intent mainActivity = new Intent(this, SearchByAreaActivity.class);
				startActivity(mainActivity);
			}
		}
		else if (item.getItemId() != R.id.menuSearchArea
				&&item.getItemId() != R.id.menuSearch
				&& item.getItemId() != R.id.menuProfile
						&& item.getItemId() != R.id.SignOut
								&& item.getItemId() != R.id.menuSignUp
										&& item.getItemId() != R.id.menuForgot
												&& item.getItemId() != R.id.menuAddPlace
														&& item.getItemId() != R.id.menuSignIn
													&& item.getItemId() != 10) {
			Intent placeIntent = new Intent(this, ViewPlaceActivity.class);
			placeIntent.putExtra("pid", item.getItemId());
			startActivity(placeIntent);
		}
		return true;
	}
}
